package com.abhimanyu.bootstrap.tasklets;

import java.util.List;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import com.abhimanyu.bootstrap.enums.BootstrapConstants;
import com.abhimanyu.bootstrap.util.JobContext;

public class DollarMessageConstructorTasklet implements Tasklet {

	@SuppressWarnings("unchecked")
	public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
			throws Exception {
		List<String> dollarRates = (List<String>) JobContext.getInstance().get(
				BootstrapConstants.DOLLAR_RATES);
		String dollarRateMessage = constructMessageText(dollarRates);
		JobContext.getInstance().put(
				BootstrapConstants.DOLLAR_RATE_TEXT_MESSAGE, dollarRateMessage);
		return RepeatStatus.FINISHED;
	}

	private String constructMessageText(List<String> rates) {
		StringBuffer sb = new StringBuffer();
		String messageText = null;
		sb.append("Dollar rate today is");
		for (String rate : rates) {
			sb.append("\n- ").append(rate);
		}

		sb.append("\n- Service by Abhimanyu's Daily Dollar Rate Notifier.");
		messageText = sb.toString();
		return messageText;
	}

}
